package com.spsy.pay.controller;

import com.alibaba.fastjson2.JSON;
import com.spsy.common.core.domain.R;
import com.spsy.pay.domain.PaymentOrder;
import com.spsy.pay.menu.PayStatusEnum;
import com.spsy.pay.order.WeiXinUtil;
import com.spsy.pay.service.IPaymentOrderService;
import com.spsy.pay.vo.OrderVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Date;
import java.util.UUID;

/**
 * @author 上杉绘梨衣 && Sakura
 * @Yu 我们都是小怪兽，总有一天会被正义的奥特曼杀死。
 * @Yu "不知道从什么时候,我开始不喜欢社交，沉浸在自己的小圈子里"
 * @date 2025/9/16-18:52
 */
@RestController
@RequestMapping("/pay")
@Slf4j
public class PayController {

    @Autowired
    private IPaymentOrderService paymentOrderService;

    @PostMapping(value = "/payOrder",consumes = MediaType.APPLICATION_JSON_VALUE)
    public R payOrder(@RequestBody OrderVo orderVo) throws Exception {

        log.info("请求参数orderVo:{}", JSON.toJSONString(orderVo),orderVo.getOrderId());

        PaymentOrder paymentOrder = PaymentOrder.builder()
                .errorCode("A")
                .orderNo(UUID.randomUUID().toString())
                .notifyUrl("url")
                .errorMsg("1")
                .expireTime(new Date())
                .payAmount(new BigDecimal(12))
                .payMethod("payMethod")
                .userIp("ip")
                .notifyStatus(PayStatusEnum.NOT_PAY.getValue())
                .payTime(new Date())
                .orderStatus((long)1)
                .build();

        int i = paymentOrderService.insertPaymentOrder(paymentOrder);

        String codeUrl = WeiXinUtil.payOrder(orderVo.getOrderId());

        return R.ok(codeUrl);
    }

}
